<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CbdViolationRecord extends BaseModel
{
    protected $table = 'cbd_violation_record';

    public function create($data)
    {
        unset($data['id']);
        return DB::table($this->table)->insert($data);
    }

    public function getList($id, $query)
    {
        $starttime = $query['begin_date'] ?? 0;
        $endtime = $query['end_date'] ?? 0;

        $whereParam = [];
        if($starttime != '' && $endtime != '') {
            $whereParam[] = ['wg_time', '>=', $starttime];
            $whereParam[] = ['wg_time', '<=', $endtime];
        }

        $list = DB::table($this->table)->where('room_id', '=', $id)
            ->where($whereParam)
            ->select('content', 'wg_time', 'id')
            ->orderBy('wg_time', 'desc')
            ->get();
        return $this->objToArray($list);
    }

    public function edit($data)
    {
        $id = $data['id'];
        unset($data['id'], $data['room_id']);
        return DB::table($this->table)->where('id', '=', $id)->update($data);
    }

    public function del($id)
    {
        return DB::table($this->table)->where('id', '=', $id)->delete();
    }
}
